package com.xydtech.ecard.imp.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.imp.domain.EcardMerchantImport;
import com.xydtech.ecard.imp.domain.EcardMerchantImportHandover;
import com.xydtech.ecard.imp.domain.EcardMerchantImportTask;
import com.xydtech.ecard.imp.mapper.EcardMerchantImportHandoverMapper;
import com.xydtech.ecard.imp.mapper.EcardMerchantImportMapper;
import com.xydtech.ecard.imp.mapper.EcardMerchantImportTaskMapper;
import com.xydtech.ecard.imp.service.IEcardMerchantImportService;
import com.xydtech.ecard.user.domain.EcardApiEcardRegistInfo;
import com.xydtech.ecard.user.mapper.EcardApiEcardRegistInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 个体工商户批量导入Service业务层处理
 *
 * @author xydtech
 * @date 2024-11-22
 */
@Service
public class EcardMerchantImportServiceImpl implements IEcardMerchantImportService {
    @Autowired
    private EcardMerchantImportMapper ecardMerchantImportMapper;
    @Autowired
    private EcardMerchantImportTaskMapper ecardMerchantImportTaskMapper;
    @Autowired
    private EcardApiEcardRegistInfoMapper ecardApiEcardRegistInfoMapper;
    @Autowired
    private EcardMerchantImportHandoverMapper ecardMerchantImportHandoverMapper;

    /**
     * 查询个体工商户批量导入
     *
     * @param id 个体工商户批量导入主键
     * @return 个体工商户批量导入
     */
    @Override
    public EcardMerchantImport selectEcardMerchantImportById(Long id) {
        return ecardMerchantImportMapper.selectEcardMerchantImportById(id);
    }

    /**
     * 根据手机号查询个体工商户导入信息
     *
     * @param phoneNumber
     * @return
     */
    @Override
    public EcardMerchantImport selectEcardMerchantImportByPhoneNumber(String phoneNumber) {
        return ecardMerchantImportMapper.selectEcardMerchantImportByPhoneNumber(phoneNumber);
    }

    /**
     * 查询所有分配任务的手机号
     * @return
     */
    @Override
    public List<String> selectPhoneNumberList() {
        return ecardMerchantImportMapper.selectPhoneNumberList();
    }

    /**
     * 查询个体工商户批量导入列表
     *
     * @param merchantImport 个体工商户批量导入
     * @return 个体工商户批量导入
     */
    @Override
    @DataScope(deptAlias = "sd")
    public List<EcardMerchantImport> selectEcardMerchantImportList(EcardMerchantImport merchantImport) {

        try {
            if (!StringUtils.objIsNullOrEmpty(merchantImport.getPhoneNumber())) {
                merchantImport.setPhoneNumber(AESEncryptionUtils.encrypt(merchantImport.getPhoneNumber(), merchantImport.getSalt()));
            }
            if (!StringUtils.objIsNullOrEmpty(merchantImport.getCardnum())) {
                merchantImport.setCardnum(AESEncryptionUtils.encrypt(merchantImport.getCardnum(), merchantImport.getSalt()));
            }
            List<EcardMerchantImport> ecardMerchantImportList = ecardMerchantImportMapper.selectEcardMerchantImportList(merchantImport);
            for (EcardMerchantImport ecardMerchantImport : ecardMerchantImportList) {
                if (!StringUtils.objIsNullOrEmpty(ecardMerchantImport.getPhoneNumber())) {
                    ecardMerchantImport.setPhoneNumber(AESEncryptionUtils.decrypt(ecardMerchantImport.getPhoneNumber(), merchantImport.getSalt()));
                }
                if (!StringUtils.objIsNullOrEmpty(ecardMerchantImport.getCardnum())) {
                    ecardMerchantImport.setCardnum(AESEncryptionUtils.decrypt(ecardMerchantImport.getCardnum(), merchantImport.getSalt()));
                }
            }
            return ecardMerchantImportList;

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    /**
     * 新增个体工商户批量导入
     *
     * @param ecardMerchantImport 个体工商户批量导入
     * @return 结果
     */
    @Override
    public int insertEcardMerchantImport(EcardMerchantImport ecardMerchantImport) {
        ecardMerchantImport.setCreateTime(DateUtils.getNowDate());
        return ecardMerchantImportMapper.insertEcardMerchantImport(ecardMerchantImport);
    }

    /**
     * 修改个体工商户批量导入
     *
     * @param ecardMerchantImport 个体工商户批量导入
     * @return 结果
     */
    @Override
    public int updateEcardMerchantImport(EcardMerchantImport ecardMerchantImport) {
        try {
            if (!StringUtils.objIsNullOrEmpty(ecardMerchantImport.getPhoneNumber())) {
                ecardMerchantImport.setPhoneNumber(AESEncryptionUtils.encrypt(ecardMerchantImport.getPhoneNumber(), ecardMerchantImport.getSalt()));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardMerchantImport.getCardnum())) {
                ecardMerchantImport.setCardnum(AESEncryptionUtils.encrypt(ecardMerchantImport.getCardnum(), ecardMerchantImport.getSalt()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return ecardMerchantImportMapper.updateEcardMerchantImport(ecardMerchantImport);
    }

    /**
     * 批量删除个体工商户批量导入
     *
     * @param ids 需要删除的个体工商户批量导入主键
     * @return 结果
     */
    @Override
    public int deleteEcardMerchantImportByIds(String ids) {
        return ecardMerchantImportMapper.deleteEcardMerchantImportByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除个体工商户批量导入信息
     *
     * @param id 个体工商户批量导入主键
     * @return 结果
     */
    @Override
    public int deleteEcardMerchantImportById(Long id) {
        return ecardMerchantImportMapper.deleteEcardMerchantImportById(id);
    }

    /**
     * 批量保存个体工商户信息
     *
     * @param ecardMerchantImportList
     * @return
     */
    @Override
    public int batchSaveOrUpdateMerchantImport(List<EcardMerchantImport> ecardMerchantImportList) {
        return ecardMerchantImportMapper.batchSaveOrUpdateMerchantImport(ecardMerchantImportList);
    }

    /**
     * 查询未分配的个体工商户数据
     *
     * @param ecardMerchantImport
     * @return
     */
    @Override
    @DataScope(deptAlias = "sd")
    public List<EcardMerchantImport> selectEcardMerchantImportNoAllocateList(EcardMerchantImport ecardMerchantImport) {
        return ecardMerchantImportMapper.selectEcardMerchantImportNoAllocateList(ecardMerchantImport);
    }

    /**
     * 转交
     *
     * @param ecardMerchantImportList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int transfer(List<EcardMerchantImport> ecardMerchantImportList) {

        List<EcardMerchantImportTask> ecardMerchantImportTaskList = new ArrayList<>();
        List<EcardMerchantImportHandover> ecardMerchantImportHandoverList = new ArrayList<>();
        for (int i = 0; i < ecardMerchantImportList.size(); i++) {
            EcardMerchantImport ecardMerchantImport = ecardMerchantImportList.get(i);
            //查询未处理的任务 sts = 0
            EcardMerchantImportTask ecardMerchantImportTask = ecardMerchantImportTaskMapper.selectUnDoneMerchantImportTaskByImportId(ecardMerchantImportList.get(i).getId());
            if (ecardMerchantImportTask != null) {
                ecardMerchantImportTask.setCmRegistId(ecardMerchantImportList.get(i).getRegistId());
                ecardMerchantImportTask.setDeptId(ecardMerchantImportList.get(i).getDeptId());
                ecardMerchantImportTaskList.add(ecardMerchantImportTask);
            }
            EcardMerchantImport oldEcardMerchantImport = ecardMerchantImportMapper.selectEcardMerchantImportById(ecardMerchantImport.getId());
            EcardApiEcardRegistInfo oldEcardRegistInfo = ecardApiEcardRegistInfoMapper.selectEcardApiEcardRegistInfoByUserCode(oldEcardMerchantImport.getUserCode());

            EcardMerchantImportHandover ecardMerchantImportHandover = new EcardMerchantImportHandover();
            ecardMerchantImportHandover.setImportId(ecardMerchantImport.getId());
            if(oldEcardRegistInfo != null) {
                ecardMerchantImportHandover.setTransferId(oldEcardRegistInfo.getRegistId());
            }
            ecardMerchantImportHandover.setTransferDeptId(oldEcardMerchantImport.getDeptId());
            ecardMerchantImportHandover.setRecipientId(ecardMerchantImport.getRegistId());
            ecardMerchantImportHandover.setRecipientDeptId(ecardMerchantImport.getDeptId());
            ecardMerchantImportHandover.setMerchantType("2");
            ecardMerchantImportHandoverList.add(ecardMerchantImportHandover);
        }
        //批量保存转交记录
        ecardMerchantImportHandoverMapper.batchInsertEcardMerchantImportHandover(ecardMerchantImportHandoverList);

        //批量转交任务
        if (ecardMerchantImportTaskList.size() > 0) {
            ecardMerchantImportTaskMapper.batchUpdateImportTask(ecardMerchantImportTaskList);
        }

        //批量修改导入数据
        return ecardMerchantImportMapper.batchUpdateMerchantImport(ecardMerchantImportList);
    }
}
